//
//  CMPasswordViewController.m
//  CouponManager
//
//  Created by BoHyun Jo on 11. 5. 15..
//  Copyright 2011 CJ오쇼핑. All rights reserved.
//

#import "CMPasswordViewController.h"
#import "Const.h"

@implementation CMPasswordViewController
@synthesize state;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
   
        
   
       // [ self.view setBackgroundColor:[UIColor redColor]];
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    switch (state) {
        case PASS_SETUP:
            stateLabel.text=@"PASS_SETUP";
            break;
        case PASS_LOGON:
            stateLabel.text=@"PASS_LOGON";
            break;
        case PASS_MODIFY:
            stateLabel.text=@"PASS_MODIFY";
            break;
        default:
            break;
    }
 
    [super viewDidLoad];
    NSLog(@"CMPasswordViewController");
    [passwordView becomeFirstResponder];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
     NSLog(@"%@",passwordView.text);
}
-(void)chkPass:(NSString*)password{
    NSLog(@"::::%@",passwordView.text);
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    switch (state) {
        case PASS_LOGON:
            if([password isEqualToString:[prefs objectForKey:@"Password"]]){
                 [self dismissModalViewControllerAnimated:YES];
            }else{
                stateLabel.text=@"틀렸음";
            }
            break;
        case PASS_SETUP:
            switch (step) {
                case 0:
                    beforePassword = [[NSString stringWithString:password]retain] ;
                    step ++;
                    break;
                case 1:
                    if([beforePassword isEqualToString:password]){
                        NSLog(@"%@",[prefs objectForKey:@"Password"]);
                        [prefs setObject:password forKey:@"Password"];
                        
                        [prefs setInteger:1 forKey:@"isPassword"]; 
                        
                        [self dismissModalViewControllerAnimated:YES];
                        
                    }else step = 0;
                    
                    break;
                    
                default:
                    break;
            }
            break;
        case PASS_MODIFY:
            switch (step) {
                case 0:
                    ;
                    break;
                case 1:
                    
                    beforePassword = [NSString stringWithString:password];
                    step ++;
                    break;
                case 2:
                    if([beforePassword isEqualToString:passwordView.text]){
                        NSLog(@"%@",[prefs objectForKey:@"Password"]);
                        [prefs setObject:password forKey:@"Password"];
                        
                        [prefs setInteger:1 forKey:@"isPassword"]; 
                        [self dismissModalViewControllerAnimated:YES];
                        
                        
                        
                    }else step = 0;
                    break;
                    
                default:
                    break;
            }
        default:
            break;
    }
    [passwordView setText:@""];
    [numLabel1 setText:@""];
    
    [numLabel2 setText:@""];
    
    [numLabel3 setText:@""];
    
    [numLabel4 setText:@""];
    
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
 
        switch (range.location) {
            case 0:
                [numLabel1 setText:string];
                break;
            case 1:
                [numLabel2 setText:string];
                break;
            case 2:
                [numLabel3 setText:string];
                break;
            case 3:
                [numLabel4 setText:string];
                break;
                
                
            default:
                break;
        }
    if([numLabel1.text length])[numLabel1 setText:@"●"];
    
    if([numLabel2.text length])[numLabel2 setText:@"●"];
    
    if([numLabel3.text length])[numLabel3 setText:@"●"];
    
    if([numLabel4.text length])[numLabel4 setText:@"●"];
    NSLog(@"%d %d %d %d",[numLabel1.text length],[numLabel2.text length],[numLabel3.text length],[numLabel4.text length]);
     NSString* TestString = [textField.text stringByAppendingString:string];
    NSLog(@"%@ %d",TestString,[TestString length]);
    if([TestString length] ==4){
        
        NSLog(@"^^");
        
        SEL selector = @selector(chkPass:);
        
        NSMethodSignature *signature = [CMPasswordViewController instanceMethodSignatureForSelector:selector];
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
        [invocation setSelector:selector];
        
        [invocation setTarget:self];
        [invocation setArgument:&TestString atIndex:2];
        [NSTimer scheduledTimerWithTimeInterval:0.5 invocation:invocation repeats:NO];
        
    }
    if([TestString length]>4)return NO;
   
    return YES;
    
}

@end
